package com.jmy.controller;

import com.github.pagehelper.Page;
import com.jmy.pageresult.PageResult;
import com.jmy.pojo.Admin;
import com.jmy.pojo.Menu;
import com.jmy.pojo.Roles;
import com.jmy.service.MenuService;
import com.jmy.service.RolesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: VE
 * @DATE: 2022/9/27 16:12
 * 描述:
 */
@RestController
public class MainController {

    private Logger logger = LoggerFactory.getLogger(MainController.class);
    @Autowired
    private RolesService rolesService;
    @Autowired
    private MenuService menuService;
    @GetMapping("main")
    public PageResult getMenuList(HttpSession session){
        Admin admin = (Admin) session.getAttribute("admin");
        logger.info("获取主页菜单");
        if(admin==null){
            return PageResult.none("未登录");
        }
        List<Roles> rolesList = rolesService.getRolesListByAid(admin.getAId());
        if(rolesList==null||rolesList.size()==0){
            return PageResult.none("未授权");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("rolesList",rolesList);
        List<Menu> firstMenu = menuService.getMenuListByRid(map);
        Map<Integer,List<Menu>> twoMenu = new HashMap<>();
        for (Menu m : firstMenu) {
            System.out.println(m.getId());
            map.put("parentId",m.getId());
            List<Menu> menus = menuService.getMenuListByRid(map);
            twoMenu.put(m.getId(),menus);
        }
        Map<String,Object> resuMap = new HashMap<>();
        resuMap.put("admin",admin);
        resuMap.put("rolesList",rolesList);
        resuMap.put("firstMenu",firstMenu);
        resuMap.put("twoMenu",twoMenu);
        return PageResult.success("请求成功",resuMap);
    }

}
